package com.cosina.ex;

import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
import android.util.Log;

public class CloseYouMouth extends Service {

	private SensorEventListener listener = new SensorEventListener(){

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			float[] values = event.values;
			AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			if(am == null)
				return ;
			if(values[1] < -130.0f){
				if(am.getRingerMode() != AudioManager.RINGER_MODE_SILENT){
					am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
					Log.e("cosina1985", "change to silent");
				}
			}else{
				if(am.getRingerMode() != AudioManager.RINGER_MODE_NORMAL){
					am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
					Log.e("cosina1985", "change to normal");
				}
			}
//			Log.e("cosina1985", "float0:" + values[0]+"," +"float1:" + values[1] + "," + "float2:" + values[2]);
		}
		
	};
	
	private List<Sensor> sensors;
	
	@Override
	public void onCreate() {
		super.onCreate();
		SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
		sensors = sm.getSensorList(Sensor.TYPE_ORIENTATION);
		
		Log.e("cosina1985", "sensors with TYPE_ORIENTATION size:" + sensors.size());
		for(Sensor each : sensors){
			sm.registerListener(listener, each, SensorManager.SENSOR_DELAY_UI);
		}
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
		sm.unregisterListener(listener);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

}
